<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['newInstallation'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['newInstallation'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $complainServiceId        = isset($_REQUEST['complainServiceId']) ? $_REQUEST['complainServiceId'] : 0;
  $categoryArray            = array();
  $newInstallationArray     = array();
  $partyId                  = "";
  $companyName              = "";
  $insertDate               = "";
  $insertDateSelected       = "";
  $targetedDate             = "";
  $description              = "";
  $quantity                 = "";
  $newInstallCount          = 0;
  $newinstallationMasterId  = isset($_REQUEST['newinstallationMasterId']) ? $_REQUEST['newinstallationMasterId'] : 0;
  $newInstallComplete       = isset($_REQUEST['newInstallComplete']) ? $_REQUEST['newInstallComplete'] : 0;
  $newInstallInComplete     = isset($_REQUEST['newInstallInComplete']) ? $_REQUEST['newInstallInComplete'] : 0;
  $isEdit                   = isset($_REQUEST['isEdit']) ? $_REQUEST['isEdit'] : 0;
  $companyNameSelected      = "";
  $categorySelected         = "";
  $subCategorySelectedAj    = "";
  $itemAjSelected           = "";
  $print                    = isset($_REQUEST['print']) ? $_REQUEST['print'] : 0;
  $printArray               = array();
  
  if(isset($_POST['insertBtn']) || isset($_POST['printBtn']))
  {
    $installationDate = $_POST['insertDateYear']."-".$_POST['insertDateMonth']."-".$_POST['insertDateDay']; 
    $closeDate        = date('Y-m-d H:i:s');
    $deactive         = "D";
  	 
   if(isset($_REQUEST['newinstallationMasterId']) &&  $_REQUEST['newinstallationMasterId'] > 0)
   {
     if($newInstallComplete == 1)
   	 {
   	   $updateNewinstallComplete = "UPDATE newinstallationmaster
   	                                   SET status = '".$deactive."', 
   	                                       closeDate = '".$closeDate."' 
   	                                 WHERE newinstallationMasterId = ".$_REQUEST['newinstallationMasterId'];
   	   $updateNewinstallUpdateQuery = mysql_query($updateNewinstallComplete);
   	   if(!$updateNewinstallUpdateQuery)                 
			   die("Insert Query Error : ". mysql_error()); 
			 else     
			 {                                     
			   header("Location:completeReport.php");
			   exit();
   	   }
   	 }
   	 elseif($newInstallInComplete == 1 || $isEdit == 1)
     {
   	   $updatenewinstallInComplete = "UPDATE newinstallationmaster
   	                                     SET partyId = ".$_REQUEST['partyId'].",
   	                                         installationDate = '".$installationDate."'
   	                                   WHERE newinstallationMasterId = ".$_REQUEST['newinstallationMasterId'];
   	   $updatenewinstallInCompleteQuery = mysql_query($updatenewinstallInComplete);
   	   if(!$updatenewinstallInCompleteQuery)                 
			   die("Insert Query Error : ". mysql_error()); 
			 else     
			 {
			   $deleteNewInstallationDetail = "DELETE from newinstallationdetail 
			                                    WHERE newinstallationMasterId = ".$_REQUEST['newinstallationMasterId'];       	                                     
				 $deleteNewInstallationDetailQuery = mysql_query($deleteNewInstallationDetail);
				 if(!$deleteNewInstallationDetailQuery)                 
				   die("Delete Query Error : ". mysql_error()); 
				 else     
				 {			                                    
           $loopCount = 0;
           while($loopCount < count($_POST['categoryId']))
           {
             $categoryId     = ($_POST['categoryId'][$loopCount] != '') ? $_POST['categoryId'][$loopCount] : 0;
             $description    = ($_POST['description'][$loopCount] != '') ? nl2br(addslashes($_POST['description'][$loopCount])) : "";
            
             if($_POST['categoryId'][$loopCount] != "")
             {
               $insertinstallationdetailDetail = "INSERT INTO newinstallationdetail (newinstallationMasterId,categoryId,description)
                                                  VALUES(".$_REQUEST['newinstallationMasterId'].",".$categoryId.",'".$description."')";
               $insertinstallationdetailDetailResult = mysql_query($insertinstallationdetailDetail);
               if(!$insertinstallationdetailDetailResult)
                 die("Insert Query Not Inserted : ".mysql_error());
             } 
             $loopCount++;      
           }
           if($newInstallInComplete == 1)
           {
           	 header("Location:serviceList.php");
           	 exit();
           }
           else
           {
             header("Location:serviceRegisList.php");             
             exit();
           }
				 }
			 }
     }
   }
   else
   {// Insert of data : Starts
     $timezone  = new DateTimeZone("Asia/Kolkata");
     $curDate = new DateTime();
     $curDate->setTimezone($timezone);
     $timeset = $curDate->format('H:i:s');
     $insertDate = $_POST['insertDateYear']."-".$_POST['insertDateMonth']."-".$_POST['insertDateDay']." ".$timeset;
     $targetDate = $_POST['targetDateYear']."-".$_POST['targetDateMonth']."-".$_POST['targetDateDay'];
     $insertDeliveryChallan = "INSERT INTO newinstallationmaster(partyId,installationDate,targetedDate)
                               VALUES('".$_POST['partyId']."','".$insertDate."','".$targetDate."')";
     $insertDeliveryChallanResult = mysql_query($insertDeliveryChallan);
     $newinstallationMasterId = mysql_insert_id();
     $loopCount = 0;
     while($loopCount < count($_POST['categoryId']))
     {
       $categoryId     = ($_POST['categoryId'][$loopCount] != '') ? $_POST['categoryId'][$loopCount] : 0;
       $description    = ($_POST['description'][$loopCount] != '') ? $_POST['description'][$loopCount] : "";
      
       if($_POST['categoryId'][$loopCount] != "")
       {
         $insertinstallationdetailDetail = "INSERT INTO newinstallationdetail (newinstallationMasterId,categoryId,description)
                                            VALUES(".$newinstallationMasterId.",".$categoryId.",'".$description."')";
         $insertinstallationdetailDetailResult = mysql_query($insertinstallationdetailDetail)or die(mysql_error());
         //if(isset($_POST['printBtn']))
         // header("Location:newInstallation.php?print=1&newinstallationMasterId=".$newinstallationMasterId);
        // else
          // header("Location:serviceList.php");
       } 
       $loopCount++;      
     }
   }
   // Insert of data : Ends
 }	
  if(isset($_REQUEST['newinstallationMasterId']) && $_REQUEST['newinstallationMasterId'] > 0)
  {
    $isEdit = 1;
    ///////////////// Listing of newInstallationMaster :  Starts
    $newInstallationMasterQuery = "SELECT newinstallationmaster.newinstallationMasterId,party.partyId,party.companyName,party.address,party.city,
                                         DATE_FORMAT(newinstallationmaster.installationDate,'%Y-%m-%d') AS installationDate
                                    FROM newinstallationmaster
                                    JOIN party ON party.partyId  = newinstallationmaster.partyId 
                                     AND newinstallationmaster.newinstallationMasterId =".$_REQUEST['newinstallationMasterId'];
                                        
    $newInstallationMasterQueryResult = mysql_query($newInstallationMasterQuery);
    if($newInstallationMaster = mysql_fetch_array($newInstallationMasterQueryResult))
    { 
      $printArray['companyName']       = $newInstallationMaster['companyName'];
      $printArray['address']           = $newInstallationMaster['address'];
      $printArray['city']              = $newInstallationMaster['city'];
      $printArray['installationDate']  = $newInstallationMaster['installationDate'];
      $companyNameSelected  = $newInstallationMaster['partyId'];
      $insertDateSelected   = $newInstallationMaster['installationDate'];
    }
    ///////////////// Listing of newInstallationMaster : End
    ///////////////// Listing of newInstallationDetail :  Starts
    $newInstallationDetailQuery = "SELECT newinstallationdetail.newinstallationDetailId,newinstallationmaster.newinstallationMasterId,
                                              category.categoryName,category.categoryId,
                                              newinstallationdetail.description
                                         FROM newinstallationdetail
                                         JOIN category ON category.categoryId = newinstallationdetail.categoryId
                                         JOIN newinstallationmaster ON newinstallationmaster.newinstallationMasterId  = newinstallationdetail.newinstallationMasterId 
                                        WHERE newinstallationdetail.newinstallationMasterId = ".$_REQUEST['newinstallationMasterId'];
    $newInstallationDetailQueryResult = mysql_query($newInstallationDetailQuery);
    $i = 0;
    while($newInstallationDetail = mysql_fetch_array($newInstallationDetailQueryResult))
    {
      $newInstallationArray[$i]['categoryName']         = $newInstallationDetail['categoryName'];
      $newInstallationArray[$i]['categoryId']           = $newInstallationDetail['categoryId'];
      $newInstallationArray[$i]['description']          = $newInstallationDetail['description'];
      
      ///for subcategory Start///
      $subCategoryQuery = "SELECT subCategoryId,categoryId,subCategoryName
                             FROM subcategory
                            WHERE categoryId = ".$newInstallationDetail['categoryId']."
                         ORDER BY subCategoryName";
      $subCategoryQueryRes = mysql_query($subCategoryQuery);
      $j = 0;
      while($subCategoryRow = mysql_fetch_array($subCategoryQueryRes))
      {
        $newInstallationArray[$i]['subCategoryId'][$j]        = $subCategoryRow['subCategoryId'];
        $newInstallationArray[$i]['subCategoryNameArray'][$j] = $subCategoryRow['subCategoryName'];
        $j++;
      }
      $i++;
    }  
    ///////////////// Listing of newInstallationDetail : End
  }
    
  ///// Select Party : Start
  $selectParty = "SELECT partyId,companyName
                    FROM party
                ORDER BY companyName";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  { 
    $partyId[$b]     = $partyRow['partyId'];
    $companyName[$b] = substr_replace($partyRow['companyName'],'...',40);
    $b++;
  }
  ///// Select Party : Ends
  
  ///// Select Category : Start
  $selectCategoryQuery = "SELECT categoryId,categoryName
	                          FROM category
	                      ORDER BY categoryName";
	$selectCategoryQueryRes = mysql_query($selectCategoryQuery);
	$j = 0;
	while($categoryRow = mysql_fetch_array($selectCategoryQueryRes))
	{
	  if($j == 0 || (isset($_REQUEST['categoryId']) && $_REQUEST['categoryId'] == $categoryRow['categoryId']))
	    $categorySelected = $categoryRow['categoryName'];
	
	  $categoryArray['categoryId'][$j]   = $categoryRow['categoryId'];
	  $categoryArray['categoryName'][$j] = $categoryRow['categoryName'];
	  $j++;
	}
  ///// Select Category : Ends
  
  include("./bottom.php");
  $smarty->assign("newInstallationArray",$newInstallationArray);
  $smarty->assign("newinstallationMasterId",$newinstallationMasterId);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("isEdit",$isEdit);
  $smarty->assign("newInstallComplete",$newInstallComplete);
  $smarty->assign("newInstallInComplete",$newInstallInComplete);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("quantity",$quantity);
  $smarty->assign("categoryArray",$categoryArray);
  $smarty->assign("description",$description);
  $smarty->assign("insertDate",$insertDate);
  $smarty->assign("insertDateSelected",$insertDateSelected);
  $smarty->assign("companyNameSelected",$companyNameSelected);
  $smarty->assign("categorySelected",$categorySelected);
  $smarty->assign("subCategorySelectedAj",$subCategorySelectedAj);
  $smarty->assign("itemAjSelected",$itemAjSelected);
  $smarty->assign("printArray",$printArray);
  if($print == 1)
    $smarty->display("newInstallationPrint.tpl");
  else
  $smarty->display("newInstallation.tpl");
}
?>